//
//  CJCommonGMFunc.m
//  CJModileGMAdapter
//
//  Created by shanghailingye on 2024/4/24.
//

#import "CJCommonGMFunc.h"

@implementation CJCommonGMFunc

// 获取当前根控制器
+ (UIViewController *)currentRootController {
    UIViewController *rootController = [self getKeyWindow].rootViewController;
    return rootController;
}

// 获取当前window
+ (UIWindow *)getKeyWindow {
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
            if (windowScene.activationState == UISceneActivationStateForegroundActive)
            {
                for (UIWindow *window in windowScene.windows)
                {
                    if (window.isKeyWindow)
                    {
                        return window;
                    }
                }
            }
        }
    } else {
        // 添加到当前window上，并置顶到最上层
        UIWindow * window = [UIApplication sharedApplication].delegate.window;
        if (window) {
            return window;
        }
        return [UIApplication sharedApplication].keyWindow;
    }
    return nil;
}

+ (double )getTimeDifferenceWithStartTime:(NSString * )startTime andEndTime:(NSString *)endTime {
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"yyyyMMddHHmmssSSS"];
      NSDate* startTimeData = [dateFormatter dateFromString:startTime];
      NSTimeInterval startTimeSp=[startTimeData timeIntervalSince1970]*1000;// *1000 是精确到毫秒，  不乘就是精确到秒
      NSDate* endTimeData = [dateFormatter dateFromString:endTime];
      NSTimeInterval endTimeSp=[endTimeData timeIntervalSince1970]*1000;
      double difference = (endTimeSp - startTimeSp);
      NSLog(@"开始时间：%@,结束时间：%@,两个时间差为%lf秒",startTime,endTime, difference);
      return difference;
}

+ (NSString * )getCurrentHourAndMinuteTime {
       NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
       // 设置想要的格式，hh与HH的区别:分别表示12小时制,24小时制
       [formatter setDateFormat:@"yyyyMMddHHmmssSSS"];
       NSDate *dateNow = [NSDate date];
     //把NSDate按formatter格式转成NSString
       NSString *currentTime = [formatter stringFromDate:dateNow];
       return currentTime;
}


@end
